package drawingBoard;

import drawingBoard.state.EndState;
import drawingBoard.state.MiddleState;
import drawingBoard.state.StartState;
import drawingBoard.memento.CareTaker;
import drawingBoard.myFile.MyImageFile;
import drawingBoard.tool.AbstractTool;

import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import javax.swing.JColorChooser;
import javax.swing.JViewport;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
/*
 * 画图工具处理逻辑类
 */
public class ImageService {
	public BufferedImage myImage;

	//初始化新drawSpace
	public void initDrawSpace(ImageFrame frame) {
		Graphics g=frame.getBufferedImage().getGraphics();  //获取画图对象
		Dimension d=frame.getDrawSpace().getPreferredSize();  //获取画布大小
		int drawWidth=(int) d.getWidth();
		int drawHeight=(int)d.getHeight();
		g.setColor(Color.WHITE);
		g.fillRect(0,0,drawWidth,drawHeight);
	}

	public void repaint(Graphics g, BufferedImage bufferedImage) {
		int drawWidth=bufferedImage.getWidth();
		int drawHeight=bufferedImage.getHeight();
		Dimension screenSize= MyImageFile.getScreenSize();
		//设置非绘画区的颜色
		g.setColor(Color.GRAY);
		g.fillRect(0,0,(int)screenSize.getWidth()*10,(int)screenSize.getHeight()*10);
		//设置拖动点的颜色
		g.setColor(Color.BLUE);
		g.fillRect(drawWidth, drawHeight,4,4);
		g.fillRect(drawWidth,(int)drawHeight/2-2,4,4);
		g.fillRect((int)drawWidth/2-2,drawHeight,4,4);
		//把缓冲的图片绘画出来
		g.drawImage(bufferedImage,0,0,drawWidth,drawHeight,null);
	}

	//创建鼠标图形
	public static Cursor createCursor(String path) {
		Image cursorImage=Toolkit.getDefaultToolkit().createImage(path);
		return Toolkit.getDefaultToolkit().createCustomCursor(cursorImage,new Point(10,10), "mycursor");
	}

	//设置JViewport
	public static void setViewport(JScrollPane scroll,JPanel panel,int width,int height) {
		JViewport viewport = new JViewport();
		panel.setPreferredSize(new Dimension(width+50,height+50));
		viewport.setView(panel);
		scroll.setViewport(viewport);
	}

	public void editColor(ImageFrame frame) {  //编辑颜色
		Color color = JColorChooser.showDialog(frame.getColorChooser(), "编辑颜色", Color.BLACK);
		color = color == null ? AbstractTool.color : color;
		AbstractTool.color = color;
		frame.getCurrentColorPanel().setBackground(color);
	}

	public void exit(ImageFrame frame) {   //退出应用
		MyImageFile myImageFile = new MyImageFile();
		myImageFile.save(false, frame);
		System.exit(0);
	}

	public void setVisible(JPanel panel) {  //设置是否可见
		boolean b = panel.isVisible()?false:true;
		panel.setVisible(b);
	}

	public void Undo(ImageFrame frame) {   //撤销
		frame.originator.getStateFromMemento(frame.careTaker.Undo());
		myImage = frame.originator.getState();
		if (CareTaker.getInstance().index != 0) {
			MiddleState middleState = new MiddleState();
			middleState.doAction(ImageFrame.getInstance().context);
		} else {
			StartState startState = new StartState();
			startState.doAction(ImageFrame.getInstance().context);
		}
	}

	public void Redo(ImageFrame frame) {  //恢复
		frame.originator.getStateFromMemento(frame.careTaker.Redo());
		myImage = frame.originator.getState();
		if (CareTaker.getInstance().index != CareTaker.getInstance().size() - 1) {
			MiddleState middleState = new MiddleState();
			middleState.doAction(ImageFrame.getInstance().context);
		} else {
			EndState endState = new EndState();
			endState.doAction(ImageFrame.getInstance().context);
		}
	}

	public void menuDo(ImageFrame frame,String cmd) {  //处理菜单事件
		if (cmd.equals("撤销")) {
			Undo(frame);
		}

		if (cmd.equals("恢复")) {
			Redo(frame);
		}

		if (cmd.equals("编辑颜色")) {
			editColor(frame);
		}

		if (cmd.equals("工具箱")) {
			setVisible(frame.getToolPanel());
		}

		if (cmd.equals("颜料盒")) {
			setVisible(frame.getColorPanel());
		}

		if (cmd.equals("新建")) {
			MyImageFile myImageFile = new MyImageFile();
			myImageFile.createGraphics(frame);
		}

		if (cmd.equals("打开")) {
			MyImageFile myImageFile = new MyImageFile();
			myImageFile.open(frame);
		}

		if (cmd.equals("保存")) {
			MyImageFile myImageFile = new MyImageFile();
			myImageFile.save(true, frame);
		}

		if (cmd.equals("退出")) {
			exit(frame);
		}
	}
}
